/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Region;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.ContainerEditPart;
import com.cea.papyrus.core.editpart.ContainerHighlightEditPolicy;
import com.cea.papyrus.core.editpart.DimensionAdaptationEditPolicy;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.statemachine.figure.SplitterFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controler for Splitter nodes in state machine diagrams.
 * These graph nodes with no UML semantics are used to introduce splitting in the regions of a state machine or state.
 * 
 * @author David Servat
 * @since 29 jan. 07
 */
public class SplitterEditPart extends ContainerEditPart{

	/**
	 * The channel.
	 */
	private int channel = 501;

	/**
	 * The constructor.
	 * 
	 * @param modelManager the associated ModelManager
	 * @param graphnode 
	 * @param graphNode the GraphNode to be managed
	 */
	public SplitterEditPart(GraphNode graphnode, ModelManager modelManager){
		super(graphnode, modelManager);

		Debug.debug(this,"call SplitterEditPart(GraphNode graphnode, ModelManager modelManager)",channel);
	}

	/**
	 * Specific edit policy installation.
	 * 
	 * @see com.cea.papyrus.common.editpart.ContainerWithSemanticEditPart#createEditPolicies()
	 */
	@Override
	protected void createEditPolicies(){
		super.createEditPolicies();

		installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new ContainerHighlightEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new StateMachineLayoutEditPolicy((XYLayout)getContentPane().getLayoutManager()));
        // AdaptDimensionEditPolicy
        installEditPolicy(DimensionAdaptationEditPolicy.DIMENSION_ADAPTATION, new ContainerDimensionAdaptationEditPolicy());
	}

	/**
	 * This method dynamically calls child EditPart during the construction chain
	 * Here this is needed because some nodes - i.e. splitters - are not linked to any UML elements.
	 * 
	 * @param model 
	 * 
	 * @return 
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	@Override
	protected EditPart createChild(Object model){
		if(model instanceof GraphNode){
			EditPart part = null;
			//there are two possible cases
			//either we have a splitter node or a region node
			//the splitter node has no semantic model
			Uml1SemanticModelBridge semanticModel = (Uml1SemanticModelBridge)((GraphNode)model).getSemanticModel();
			if(semanticModel == null){
				part = new SplitterEditPart((GraphNode)model, modelManager);
			}
			else if(semanticModel.getElement() instanceof Region){
				part = new RegionEditPart((GraphNode)model, modelManager);
			}
			part.setModel(model);
			return part;
		}
		return super.createChild(model);
	}

	/**
	 * Calls for the appropriate figure associated to the element.
	 * 
	 * @return 
	 * 
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure(){
		return new SplitterFigure();
	}

	/**
	 * This method is called whenever graphical changes need to be performed.
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals(){
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);

		SplitterFigure f = (SplitterFigure) getFigure();

		//set colors
		f.refreshColors(getGraphNode());

		((GraphicalEditPart) getParent()).setLayoutConstraint(this, f, r);
	}
}